package sweetsaremeat.beehive.Model;

import java.util.Random;

import sweetsaremeat.beehive.View.Camera;

public class Game 
{
	public Map m_map;
	
	public Player m_player;
	
	public long m_randomSeed;
	
	private int m_visionRange = 2;
	private int m_fogOfWallThickness = 2;
	
	public Game ()
	{
		
		/*
		Random m_random = new Random();
		for(int i=0; true; i++)
		{
		m_randomSeed = i;
		System.out.println("Seed: " + i);
		Node.UniqueId = 0;
		m_map = new Map(m_randomSeed);
		}*/
		/*
		m_randomSeed = 54367;//256262;//40732;//10095;//8334;
		m_map = new Map(m_randomSeed);
		
		//create the player and set the player's current hexagon
		m_player = new Player(m_map.m_startNode);
		*/
		Random m_random = new Random(8);
		int f_dir = 0;
		int i = 0, n = 0;
		
		/*
		for(i=0; i < 17859; i++)
		{
			System.out.println("Move #" + i);
			f_dir = m_random.nextInt(6);
			
			if(i == 36)
			{
				i += 0;
			}
			
			//meh
			MovePlayer(f_dir);
		}*/
		
		
		for(n=65849; true; n++)
		{
			m_random = new Random(n);
			
			m_randomSeed = n;
			m_map = new Map(m_randomSeed);
			
			//create the player and set the player's current hexagon
			m_player = new Player(m_map.m_startNode);
			
			for(i=0; i < 43; i++)
			{
				while(true)
				{
					
					f_dir = m_random.nextInt(6);
					
					if(m_player.m_node.m_connectedTo[f_dir])
					{
						System.out.println("Seed #" + n + "   Move #" + i);
						
						if(i == 42)
						{
							i += 0;
						}
						
						PerfectMovePlayer(f_dir);
						break;
					}
				}
			}
			break;
		}
		/*
		m_random = new Random(2283);
		for(i=0; i < 91; i++)
		{
			System.out.println("Seed #" + n + "   Move #" + i);
			f_dir = m_random.nextInt(6);
			
			if(i == 90)
			{
				i += 0;
			}
			
			//meh
			MovePlayer(f_dir);
		}
		*/
		/*
		f_dir = 4;
		for(i=0; i < 8; i++)
		{
			MovePlayer(f_dir);
		}
		
		f_dir = 0;
		for(i=0; i < 8; i++)
		{
			MovePlayer(f_dir);
		}
		
		f_dir = 1;
		for(i=0; i < 7; i++)
		{
			MovePlayer(f_dir);
		}
		
		f_dir = 2;
		for(i=0; i < 4; i++)
		{
			MovePlayer(f_dir);
		}
		
		f_dir = 4;
		for(i=0; i < 4; i++)
		{
			MovePlayer(f_dir);
		}*/
		
		System.out.println(m_player.m_node);
		//System.out.println("Next Direction= " + m_random.nextInt(6));
	}
	
	public void MovePlayer(int a_direction)
	{
		
		//increase the map
		m_map.IncreaseMap(m_player.m_node, a_direction, m_visionRange + m_fogOfWallThickness);
		
		//move the player
		m_player.SetNode(m_player.m_node.m_neighbors[a_direction]);
		
		//make nodes visible
		Node[] f_visibleNodes = m_map.GetNodeLine(m_player.m_node, a_direction, m_visionRange);
		for(int i=0; i < f_visibleNodes.length; i++)
		{
			f_visibleNodes[i].m_visible = true;
		}
	}
	
	
	public void PerfectMovePlayer(int a_direction)
	{
		int i;
		boolean f_needToIncrease = false;
		Node f_currentNode;
		
		//move the player
		m_player.SetNode(m_player.m_node.m_neighbors[a_direction]);
		//get the node line of the new vision
		Node[] f_visibleNodes = m_map.GetNodeLine(m_player.m_node, a_direction, m_visionRange);
		
		f_currentNode = f_visibleNodes[0];
		i = 0;
		while(i < m_fogOfWallThickness && f_currentNode.m_neighbors[a_direction] != null)
		{
			f_currentNode = f_currentNode.m_neighbors[a_direction];
			i++;
		}
		
		if(i < m_fogOfWallThickness)
		{
			//increase the map
			m_map.PerfectIncreaseMap(m_player.m_node, a_direction);
		}
		
		//make the nodes visible
		for(i=0; i < f_visibleNodes.length; i++)
		{
			f_visibleNodes[i].m_visible = true;
		}
	}
}
